﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NCP.Core.Extensions
{

    public static class ConvertStringExtensions
    {
        /// <summary>
        /// 判断对象是否为null
        /// </summary> 
        public static bool IsNull(this object thisValue)
        {
            return (thisValue == null || thisValue == DBNull.Value);
        }



        public static string AsString(this object thisValue)
        {
            return !thisValue.IsNull() ? thisValue.ToString() : null;
        }

        public static bool ToBoolean(this object thisValue)
        {
            if (thisValue is bool)
                return (bool)thisValue;
            if (thisValue != null && thisValue != DBNull.Value)
                return thisValue.ToString() == "1" || thisValue.ToString().ToLower() == "true";
            return false;
        }

        public static bool IsDateTime(this object thisValue)
        {
            return !thisValue.IsNull() && DateTime.TryParse(thisValue.ToString(), out DateTime result);
        }

        public static DateTime? AsDateTime(this object thisValue)
        {
            if (!thisValue.IsNull() && DateTime.TryParse(thisValue.ToString(), out DateTime result))
                return result;
            else
                return null;
        }

        public static DateTime AsDateTime(this object thisValue, DateTime defaultValue)
        {
            if (!thisValue.IsNull() && DateTime.TryParse(thisValue.ToString(), out DateTime result))
                return result;
            else
                return defaultValue;
        }

        public static decimal? AsDecimal(this object thisValue)
        {
            if (!thisValue.IsNull() && decimal.TryParse(thisValue.ToString(), out decimal result))
                return result;
            else
                return null;
        }

        public static decimal AsDecimal(this object thisValue, decimal defaultValue)
        {
            if (!thisValue.IsNull() && decimal.TryParse(thisValue.ToString(), out decimal result))
                return result;
            else
                return defaultValue;
        }

        public static double? AsDouble(this object thisValue)
        {
            if (!thisValue.IsNull() && double.TryParse(thisValue.ToString(), out double result))
                return result;
            else
                return null;
        }

        public static double AsDouble(this object thisValue, double defaultValue)
        {
            if (!thisValue.IsNull() && double.TryParse(thisValue.ToString(), out double result))
                return result;
            else
                return defaultValue;
        }

        public static bool IsInt(this object thisValue)
        {
            return !thisValue.IsNull() && int.TryParse(thisValue.ToString(), out int result);
        }

        public static int? AsInt(this object thisValue)
        {
            if (!thisValue.IsNull() && int.TryParse(thisValue.ToString(), out int result))
                return result;
            else
                return null;
        }

        public static int AsInt(this object thisValue, int defaultValue)
        {
            if (thisValue.IsNull())
                return defaultValue;

            if (thisValue is bool)
                return (bool)thisValue ? 1 : 0;

            if (thisValue.GetType().IsEnum)
                return (int)thisValue;

            if (int.TryParse(thisValue.ToString(), out int result))
                return result;
            else
                return defaultValue;
        }
        public static long? AsLong(this object thisValue)
        {
            if (!thisValue.IsNull() && long.TryParse(thisValue.ToString(), out long result))
                return result;
            else
                return null;
        }

        public static long AsLong(this object thisValue, int defaultValue)
        {
            if (!thisValue.IsNull() && long.TryParse(thisValue.ToString(), out long result))
                return result;
            else
                return defaultValue;
        }


        /// <summary>
        /// 转换为时间戳
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static long DateTimeToTimeStamp(this DateTime thisValue)
        {
            return (long)(thisValue - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds;
        }

        /// <summary>
        /// 时间戳转时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime TimeStampToDateTime(double timeStamp)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddSeconds(timeStamp);
        }

        public static string JsonToString(this object thisValue)
        {
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            };
            return JsonConvert.SerializeObject(thisValue, timeConverter);
        }

        public static T JsonParse<T>(this string thisValue)
        {
            return JsonConvert.DeserializeObject<T>(thisValue);
        }

    }
}
